package info.motteke.kurumi.swt;

import info.motteke.kurumi.VoiceInput;
import info.motteke.kurumi.VoiceInputListener;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

class SwtVoiceInput implements VoiceInput {

    private final Composite base;

    private final Text input;

    private final Label count;

    private final Button button;

    private final Button res;

    private final List<VoiceInputListener> listeners = new ArrayList<VoiceInputListener>();

    public SwtVoiceInput(Composite parent) {
        base = new Composite(parent, SWT.NULL);
        base.setLayout(new GridLayout(4, false));
        base.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        res = new Button(base, SWT.CHECK | SWT.RIGHT);
        res.setText("返信");
        input = new Text(base, SWT.SINGLE | SWT.BORDER);
        count = new Label(base, SWT.NULL);
        count.setText("150");
        button = new Button(base, SWT.NULL);
        button.setText("送信");

        res.setLayoutData(new GridData());
        input.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        count.setLayoutData(new GridData());
        button.setLayoutData(new GridData());
        input.addModifyListener(new InputChangeListener(count));
        button.addSelectionListener(new SendButtonListener(this));
    }

    public void setLayoutData(Object layoutData) {
        base.setLayoutData(layoutData);
    }

    public Control getBase() {
        return base;
    }

    public Text getText(){
        return input;
    }

    @Override
    public String getInputText() {
        return input.getText();
    }

    @Override
    public void setInputText(String text) {
        input.setText(text);
    }

    @Override
    public boolean isComment() {
        return res.getSelection();
    }

    @Override
    public void clear() {
        input.setText("");
        res.setSelection(false);
    }

    @Override
    public String getFileName() {
        return null;
    }

    @Override
    public InputStream getImage() {
        return null;
    }

    @Override
    public boolean hasImage() {
        return false;
    }

    @Override
    public void addVoiceInputListener(VoiceInputListener listener) {
        listeners.add(listener);
    }

    public List<VoiceInputListener> getListeners() {
        return listeners;
    }
}

/* package */ class InputChangeListener implements ModifyListener {

    private final Label charCounter;

    public InputChangeListener(Label charCounter) {
        this.charCounter = charCounter;
    }

    @Override
    public void modifyText(ModifyEvent modifyevent) {
        Text text = (Text) modifyevent.widget;

        charCounter.setText(String.valueOf(150 - text.getText().length()));
    }
}
